﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Windows.Input;
using WpfDataStreamDemo.Repository.Streaming.Demo.Repository;
using WpfDataStreamDemo.Utility;

namespace WpfDataStreamDemo.ViewModel
{
    public class StreamDataViewModel
    {
        public ObservableCollection<DataRow> Test
        {
            get
            {
                var result = new ObservableCollection<DataRow>();
                RunAsync(() => StreamRepository.GetTest().ForEach(result.AddOnUi));
                return result;
            }
        }

        public ObservableCollection<DataRow> TestWithLatency
        {
            get
            {
                var result = new ObservableCollection<DataRow>();
                RunAsync(() => StreamRepository.GetTestWithLatency().ForEach(result.AddOnUi));
                return result;
            }
        }

        public ICommand Pause
        {
            get { return new DelegateCommand(o => Extensions.Pause()); }
        }

        public ICommand Continue
        {
            get { return new DelegateCommand(o => Extensions.Continue()); }
        }

        private void RunAsync(Action action)
        {
            var bgw = new BackgroundWorker();
            bgw.DoWork += (sender, args) => action();
            bgw.RunWorkerAsync();
        }

    }
}
